package cn.net.shuxing.yifu.mysiteforme.admin.base;

import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.mysiteforme.admin.realm.AuthRealm;
import cn.net.shuxing.yifu.mysiteforme.admin.realm.AuthRealm.ShiroUser;
import cn.net.shuxing.yifu.mysiteforme.admin.service.UploadService;
import com.aliyun.oss.OSSClient;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.util.Date;

/**
 * Created by wangl on 2017/11/25.

 */
@Component
public class MySysUser {
    /**
     * 取出Shiro中的当前用户LoginName.
     */

    private static MySysUser mySysUser;

    @Autowired
    @Qualifier("ossService")
    private UploadService ossService;

//    @Autowired
//    @Qualifier("ossService")
//    private static UploadService ossServiceStatic;

    @PostConstruct
    public void init() {
        mySysUser = this;
        mySysUser.ossService = this.ossService;
    }

    public static String icon() {
        return ShiroUser().getIcon();
    }

    public static Long id() {
        return ShiroUser().getId();
    }

    public static String loginName() {
        return ShiroUser().getloginName();
    }

    public static String nickName() {
        return ShiroUser().getNickName();
    }

    public static AuthRealm.ShiroUser ShiroUser() {
        Object user = SecurityUtils.getSubject().getPrincipal();
        if (user == null)
            return null;
        Class<?> userClass = user.getClass();
        try {
            long id = (long) userClass.getDeclaredField("id").get(user);
            String loginName = (String) userClass.getDeclaredField("loginName").get(user);
            String nickName = (String) userClass.getDeclaredField("nickName").get(user);
            String icon = (String) userClass.getDeclaredField("icon").get(user);
            if (icon != null) {

                String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
                String accessKeyId = "LTAIQb8xLiRdju3q";
                String accessKeySecret = "1G6AFqff9UyONRy8zroKpu8UZjfnZj";

// 创建OSSClient实例。
                OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

// 设置URL过期时间为1小时。
                Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
                URL url = ossClient.generatePresignedUrl(OssConstant.ossBucketName, icon, expiration);

// 关闭OSSClient。
                ossClient.shutdown();
                icon = url.toString();

            }
            AuthRealm.ShiroUser shiroUser = new AuthRealm.ShiroUser(id, loginName, nickName, icon);
            return shiroUser;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}
